package com.ebs.md.fxmonitor.performance;

import java.util.List;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import com.ebs.commons.logger.MessageText;

import com.ebs.commons.comp.IParsedPayload;

/**
 * <p>Title: NetLink</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author ygrinberg
 * @version 1.0
 */
public class DumpPerformance implements MessageListener
{
    String m_messageDiscriminatorKey;
    Map<String,List<LogFormatter>> m_transformers;

    Logger s_logger = Logger.getLogger(DumpPerformance.class);

    public DumpPerformance() {}

    public void setMessageDiscriminatorKey(String theDiscriminatorKey)
    {
        m_messageDiscriminatorKey = theDiscriminatorKey;
    }

    public String getMessageDiscriminatorKey()
    {
        return m_messageDiscriminatorKey;
    }

    public void setTransformers(Map theTransformers)
    {
        m_transformers = theTransformers;
    }


     public void onMessage(Message message)
     {
         try
         {
             if((message == null) ||
                (m_messageDiscriminatorKey == null) ||
                (m_transformers == null))
             {
                 return;
             }
             IParsedPayload payload = (IParsedPayload) ((ObjectMessage) message).
                                      getObject();
             String discriminator = payload.getParsedStringField(m_messageDiscriminatorKey);
             List<LogFormatter> transformers = m_transformers.get(discriminator);
             if((transformers == null) || transformers.size() <= 0)
             {
                 return;
             }
             StringBuilder theLogData = new StringBuilder();
             for (LogFormatter nextTransformer : transformers)
             {
                 nextTransformer.format(payload, theLogData);
             }
             s_logger.info(theLogData.toString());
         }
         catch(JMSException jmse)
         {
             s_logger.error(new MessageText("NETLINK_0100004", message), jmse);
             /** @todo throw something ?*/
        }
     }


}
